<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from "axios";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      mapData: {}, // 所获地图矢量的缓存
    };
  },
  // created() {
  //   this.$socket.registerCallBack("mapData", this.getData);
  // },
  mounted() {
    this.initChart();
    // this.$socket.send({
    //   action: "getData",
    //   sockerType: "mapData",
    //   chartName: "map",
    //   value: "",
    // })
    this.getData();
    window.addEventListener("resize", this.screenAdpter);
    this.screenAdpter();
  },
  distoryed() {
    window.removeEeventListener("resize", this.screenAdpter);
    // this.$socket.unRegisterCallBack("mapData");
  },
  computed: {},
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, "chalk");
      // 获取中国地图的矢量数据
      const ret = await this.$http.get(
        "map/china"
      );
      console.log(ret)
      this.$echarts.registerMap("china", ret.data);
      const initOption = {
        title: {
          text: "▎商家分布",
          left: 20,
          top: 20,
        },
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle: {
            areaColor: "#2172BF",
            borderColor: "#333",
          },
        },
        legend: {
          left: "5%",
          bottom: "5%",
          orient: "vertical",
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("click", async (arg) => {
        // arg.name 得到所点击的省份，是中文
        const provinceInfo = getProvinceMapInfo(arg.name);

        // 判断当前所点击的省份数据，在mapData中是否存在
        if (!this.mapData[provinceInfo.key]) {
          const ret = await this.$http.get(
            "" + provinceInfo.path
          );
          this.mapData[provinceInfo.key] = ret.data;
          this.$echarts.registerMap(provinceInfo.key, ret.data);
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key,
          },
        };
        this.chartInstance.setOption(changeOption);
      });
    },
    async getData() {
      const { data: ret } = await this.$http.get("map");
      this.allData = ret;
      // console.log(this.allData);
      this.updataChart();
    },
    updataChart() {
      // 处理图标需要的数据
      // 图例的数据
      const legendArr = this.allData.map((item) => {
        return item.name;
      });
      const seriesArr = this.allData.map((item) => {
        // 一个图表下的所有散点数据
        // 如果需要在地图中显示散点数据，需要配置coordinateSystem: 'geo'
        return {
          type: "effectScatter",
          name: item.name,
          data: item.children,
          coordinateSystem: "geo",
          rippleEffect: {
            scale: 5,
            brushType: "stroke",
          },
        };
      });

      const dataOption = {
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdpter() {
      const titleFontsize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontsize,
          },
        },
        legend: {
          itemWidth: titleFontsize / 2,
          itemHeight: titleFontsize / 2,
          itemGap: titleFontsize / 2,
          textStyle: {
            fontSize: titleFontsize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    // 回到中国地图
    revertMap() {
      const revertOption = {
        geo: {
          map: "china",
        },
      };
      this.chartInstance.setOption(revertOption);
    },
  },
};
</script>

<style lang="less"></style>
